<template>
  <div class="feedback-container">
    <h2>用户反馈列表</h2>

    <el-card v-for="fb in processedFeedbacks" :key="fb.id" class="feedback-item">
      <div class="header">
        <div class="rating">
          <el-rate v-model="fb.rating" disabled show-score text-color="#ff9900" />
        </div>
        <span class="time">{{ formatDate(fb.created_at) }}</span>
      </div>

      <div class="content">
        <el-text type="info">反馈内容：</el-text>
        <p class="content-text">{{ fb.content }}</p>
      </div>

      <div v-if="fb.contact" class="contact">
        <el-text type="info">联系方式：</el-text>
        {{ fb.contact }}
      </div>

      <div v-if="fb.fullImages?.length" class="images">
        <!-- 改为 fullImages -->
        <el-image
          v-for="(img, idx) in fb.fullImages"
          :key="img.url"
          :src="img.url"
          :preview-src-list="img.previewList"
          :initial-index="idx"
          fit="cover"
          class="image-item"
          hide-on-click-modal
          lazy
          :loading="img.loading"
        >
          <template #placeholder>
            <div class="image-placeholder">
              <el-icon :size="20"><Loading /></el-icon>
            </div>
          </template>
        </el-image>
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { Loading } from '@element-plus/icons-vue'
import api from '../axios/api'

const feedbacks = ref([])
const baseUrl = import.meta.env.VITE_API_BASE_URL

// 预处理反馈数据
const processedFeedbacks = computed(() => {
  return feedbacks.value.map(fb => ({
    ...fb,
    fullImages:
      fb.images?.map((img, idx, arr) => ({
        url: getFullImageUrl(img),
        previewList: arr.map(getFullImageUrl), // 预生成预览列表
        loading: true,
      })) || [],
  }))
})

const formatDate = dateStr => {
  return new Date(dateStr).toLocaleString()
}

const getFullImageUrl = relativePath => {
  if (!relativePath) return ''
  if (/^(https?:)?\/\//.test(relativePath)) return relativePath
  return `${baseUrl}${relativePath.startsWith('/') ? '' : '/'}${relativePath}`
}

// 图片预加载
const preloadImages = (urls) => {
  urls.forEach(url => {
    const img = new Image();
    img.src = url;
    img.onload = () => {
      // 使用 find 确保准确找到对应的 feedback 和 image
      feedbacks.value = processedFeedbacks.value
      const feedback = feedbacks.value.find(fb => 
        fb.images.some(relativePath => getFullImageUrl(relativePath) === url)
      );
      if (feedback) {
        const imageIndex = feedback.images.findIndex(relativePath => 
          getFullImageUrl(relativePath) === url
        );
        feedback.fullImages[imageIndex].loading = false;
      }
    };
  });
};

onMounted(async () => {
  try {
    const { data } = await api.getFeedback()
    feedbacks.value = data

    // 提取所有图片URL进行预加载
    const allUrls = data.flatMap(fb => fb.images?.map(img => getFullImageUrl(img)) || [])
    preloadImages(allUrls)
  } catch (err) {
    ElMessage.error('获取反馈失败')
  }
})
</script>

<style scoped>
.feedback-container {
  max-width: 1200px;
  margin: 20px auto;
  padding: 20px;
}

.feedback-item {
  margin-bottom: 20px;
  transition: none;
}

.feedback-item:hover {
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
}

.rating {
  display: flex;
  align-items: center;
}

.time {
  color: #999;
  font-size: 0.9em;
}

.content-text {
  margin: 10px 0;
  line-height: 1.6;
  color: #666;
}

.images {
  margin-top: 15px;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
  gap: 10px;
}

.image-item {
  width: 100%;
  height: 120px;
  border-radius: 4px;
  cursor: pointer;
  transition: transform 0.3s;
}

.contact {
  margin-top: 10px;
  color: #666;
}

/* 新增CSS变量 */
:deep() {
  --primary-color: #409EFF;
  --text-primary: #303133;
  --text-regular: #606266;
  --text-secondary: #909399;
  --border-color: #e6ebf5;
}

.feedback-container {
  max-width: 1200px;
  margin: 40px auto;
  padding: 0 24px;
}

h2 {
  color: var(--text-primary);
  font-size: 24px;
  font-weight: 600;
  margin-bottom: 32px;
  padding-bottom: 12px;
  border-bottom: 2px solid var(--primary-color);
  display: inline-block;
}

.feedback-item {
  margin-bottom: 24px;
  border-radius: 12px;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  border: 1px solid var(--border-color);
  
  &:hover {
    /* transform: translateY(-2px); */
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.08);
  }

  :deep(.el-card__body) {
    padding: 20px;
  }
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
  
  .rating {
    :deep(.el-rate) {
      --el-rate-icon-size: 24px;
    }
    
    :deep(.el-rate__text) {
      font-size: 14px;
      margin-left: 8px;
      color: var(--text-regular);
    }
  }

  .time {
    color: var(--text-secondary);
    font-size: 13px;
    font-family: monospace;
  }
}

.content {
  background: #f8f9fc;
  border-radius: 8px;
  padding: 16px;
  margin: 12px 0;
  /* 支持换行 */
  white-space: pre-wrap;
  /* 支持markdown */
  white-space: pre-wrap;
  word-break: break-all;

  &-text {
    color: var(--text-regular);
    margin: 8px 0 0;
    line-height: 1.7;
    font-size: 14px;
  }
}

.contact {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--text-secondary);
  font-size: 14px;
  margin-top: 12px;
  padding: 12px 0;
  border-top: 1px dashed var(--border-color);

  :deep(.el-text) {
    margin-right: 4px;
  }
}

.images {
  margin-top: 16px;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 12px;
  
  .image-item {
    width: 100%;
    height: 150px;
    border-radius: 8px;
    overflow: hidden;
    transition: transform 0.3s ease;
    background: linear-gradient(145deg, #f3f4f6, #ffffff);
    
    /* &:hover {
      transform: scale(1.03);
    } */
    
    :deep(.el-image__inner) {
      object-fit: cover;
    }
  }
}

.image-placeholder {
  @keyframes shimmer {
    100% { background-position: 200% 0; }
  }
  
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(
    90deg,
    #f3f4f6 25%,
    #e5e7eb 50%,
    #f3f4f6 75%
  );
  background-size: 200% 100%;
  animation: shimmer 1.5s infinite;
}
</style>
